package pl.swspiz;

import java.io.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.*;
import javax.servlet.http.*;


/**
 * 
 */

public class UnitEditServlet extends HttpServlet {
    Connection currentCon = null;
    ResultSet rs = null;
   Statement stmt = null;
    Unit unit =null ;
    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        String id = request.getParameter("id");
        String wsp = request.getParameter("wsp");
        String nazwa = request.getParameter("nazwa");
    try {
      currentCon = ConnectionManager.getConnection();
      stmt = currentCon.createStatement();
      
      rs = stmt.executeQuery("select * from jednostki where nazwa='"+id+"'");
      if (rs.next()) // jesli wpis o takim id , nazwie juz istnieje, uaktualnij
      stmt.executeUpdate("UPDATE jednostki SET nazwa='"+nazwa+"', wsp='"+wsp+"' WHERE nazwa='"+id+"'");
      else
        stmt.executeUpdate("INSERT INTO jednostki(nazwa, wsp) VALUES ('"+nazwa+"','"+wsp+"')");
      //request.setAttribute("unit", unit);

      } catch(Exception e){
      System.out.println("Wystapil blad SQL;"+e);
      }
      finally {
         if (rs != null)	{
	            try {
	                rs.close();
	            } catch (Exception e) {}
	            rs = null;
	        }

	        if (stmt != null) {
	            try {
	                stmt.close();
	            } catch (Exception e) {}
	            stmt = null;
	        }
          if (currentCon != null) {

	            try {
	                currentCon.close();
	            } catch (Exception e) {
	            }
	            currentCon = null;

	        }
      } //finally

    response.sendRedirect(request.getContextPath() + "/listRecords");
    }
    
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

    //get parameter do of the request
    String action = request.getParameter("do");
    String id = request.getParameter("id");
    //PrintWriter out = response.getWriter();
    
    //Pobranie danych z bazy i utworzenie obiektu Unit
    if (action !=null && id != null)
    try {
      currentCon = ConnectionManager.getConnection();
      stmt = currentCon.createStatement();

      //============
//       if(action.equals("add")){
//        response.sendRedirect(request.getContextPath() + "/unitEdit.jsp");
//		} else
      if (action.equals("edit")) {
      rs = stmt.executeQuery("select * from jednostki where nazwa='"+id+"'");
      if (rs.next())
      unit = new Unit(rs.getString("nazwa"), rs.getFloat("wsp"));
      request.setAttribute("unit", unit); // dane do wyswietlenia na stronie w polach edycji

      RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/unitEdit.jsp");
      dispatcher.forward(request, response);

      }else if (action.equals("delete")){
    
        if (request.getSession(false).getAttribute("currentSessionUser") !=null){
	stmt.executeUpdate("DELETE from jednostki where nazwa='"+id+"'");
        response.sendRedirect(request.getContextPath() + "/listRecords");
        } else
            response.sendRedirect(request.getContextPath() + "/LoginPage.jsp");
        

    } //if


      } catch(Exception e){
      System.out.println("Wystapil blad SQL;"+e);
      }
      finally {
         if (rs != null)	{
	            try {
	                rs.close();
	            } catch (Exception e) {}
	            rs = null;
	        }

	        if (stmt != null) {
	            try {
	                stmt.close();
	            } catch (Exception e) {}
	            stmt = null;
	        }
          if (currentCon != null) {

	            try {
	                currentCon.close();
	            } catch (Exception e) {
	            }
	            currentCon = null;

	        }
      } //finally 
    else
    {response.sendRedirect(request.getContextPath() + "/listRecords");}
    }
    
    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "ListPerson servlet";
    }
    // </editor-fold>
}
